//package com.kira.scaffoldmvc.Aop;
//
//import cn.hutool.cache.CacheUtil;
//import cn.hutool.cache.impl.TimedCache;
//import jakarta.servlet.http.HttpServletRequest;
//import lombok.extern.slf4j.Slf4j;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Pointcut;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import java.io.BufferedReader;
//import java.io.IOException;
//import java.io.InputStreamReader;
//import java.time.LocalDateTime;
//import java.time.format.DateTimeFormatter;
//import java.util.concurrent.ArrayBlockingQueue;
//import java.util.concurrent.ThreadPoolExecutor;
//import java.util.concurrent.TimeUnit;
//
//@Aspect
//@Slf4j
//@Component
//public class GatewayReportLogAspect {
//
//    //设置一个永不过期的Cache
//    private TimedCache<String, String> cache = CacheUtil.newTimedCache(Integer.MAX_VALUE * 1000L);
//
//    @Value("${name}")
//    private String environmentName;
//
//    private static final int CORE_POOL_SIZE = 2;//核心线程数
//    private static final int MAX_POOL_SIZE = 2;//最大线程数
//    private static final int QUEUE_CAPACITY = 5000;//队列的最大数量
//    private static final Long KEEP_ALIVE_TIME = 1L;//保持连接的时间
//    private static final ThreadPoolExecutor gatewayReportLogExecutor = new ThreadPoolExecutor(
//            CORE_POOL_SIZE,
//            MAX_POOL_SIZE,
//            KEEP_ALIVE_TIME,
//            TimeUnit.SECONDS,
//            new ArrayBlockingQueue<>(QUEUE_CAPACITY),
//            new ThreadPoolExecutor.CallerRunsPolicy()
//    );
//
//    //*.*(..)表示所有类下的所有方法
//    @Pointcut("execution(public * com.kira.scaffoldmvc.Controller.*.*(..))")
//    public void pointer() {
//    }
//
//    @Around(value = "pointer()")
//    public Object aroundMethod(ProceedingJoinPoint point) throws Throwable {
//
//        //拿到Request请求,从而获取Request请求里面的信息
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//
//        // 获取请求头数据
//        String url = request.getRequestURI();
//        String source = request.getHeader("x-real-ip");
//        String appId = request.getHeader("app_id");
//        String traceId = request.getHeader("header-rid");
//        String formattedDateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//
//        //异步执行打印日志
//        gatewayReportLogExecutor.execute(() -> {
//            String resourceName = null;
//            if (cache.get(source) == null) {
//                Process process = null;
//                try {
//                    process = Runtime.getRuntime().exec("host " + source);
//                    // 读取命令输出
//                    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
//                    String line;
//                    StringBuilder output = new StringBuilder();
//                    while (true) {
//                        if (!((line = reader.readLine()) != null)) {
//                            break;
//                        }
//                        output.append(line).append("\n");
//                    }
//
//                    String strs[] = output.toString().split(" ");
//                    resourceName = strs[strs.length - 1].trim().substring(0, strs[strs.length - 1].trim().length() - 1);
//                    //设置key的过期时间为3分钟
//                    cache.put(source, resourceName, 3 * 60 * 1000);
//                } catch (IOException e) {
//                    log.info("处理失败:{}", e.getMessage());
//                }
//
//            } else {
//                resourceName = cache.get(source);
//            }
//            log.info("sec_gateway_report_data||timestamp={}||traceId={}||source={}||resourceName={}||url={}||appId={}||environmentName={}",
//                    formattedDateTime, traceId, source, resourceName, url, appId, environmentName);
//        });
//
//        Object result;
//        try {
//            result = point.proceed(); // 执行目标方法
//        } catch (Throwable e) {
//            log.error("请求处理失败：{}", e.getMessage());
//            throw e;
//        }
//
//        return result;
//    }
//
//}
